/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openjawtech.xscraper.scraper.runtime.xml;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import com.openjawtech.xscraper.schema.scrape_config.ScrapeGroupsType;
import com.openjawtech.xscraper.scraper.exception.ConfigurationException;
import java.io.InputStream;
import java.net.URL;

/**
 *
 * @Created on 25-Apr-2009, 06:37:28
 * @author Wenbo.Sun
 */
public class ScrapeXDB {

    private static final Log log = LogFactory.getLog(ScrapeXDB.class);

    public ScrapeGroupsType unMarshall(File xmlDoc) {
        log.debug("unmarshall file -- " + xmlDoc.getName());
        ScrapeGroupsType sgt = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance("com.openjawtech.xscraper.schema.scrape_config");
            Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
            JAXBElement<ScrapeGroupsType> scrapeGroupsElement = (JAXBElement<ScrapeGroupsType>) unMarshaller.unmarshal(xmlDoc);
            sgt = scrapeGroupsElement.getValue();
        } catch (JAXBException ex) {
            log.error("unmarshall file failed!", ex);
        }
        return sgt;
    }

    public ScrapeGroupsType unMarshall(InputStream xmlDoc) {
        log.debug("unmarshall input stream ...");
        ScrapeGroupsType sgt = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance("com.openjawtech.xscraper.schema.scrape_config");
            Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
            JAXBElement<ScrapeGroupsType> scrapeGroupsElement = (JAXBElement<ScrapeGroupsType>) unMarshaller.unmarshal(xmlDoc);
            sgt = scrapeGroupsElement.getValue();
        } catch (JAXBException ex) {
            log.error(new ConfigurationException("unmarshall input stream failed!", ex));
        }
        return sgt;
    }

    public ScrapeGroupsType unMarshall(URL xmlDoc) {
        log.debug("unmarshall url -- " + xmlDoc.getPath());
        ScrapeGroupsType sgt = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance("com.openjawtech.xscraper.schema.scrape_config");
            Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
            JAXBElement<ScrapeGroupsType> scrapeGroupsElement = (JAXBElement<ScrapeGroupsType>) unMarshaller.unmarshal(xmlDoc);
            sgt = scrapeGroupsElement.getValue();
        } catch (JAXBException ex) {
            log.error(new ConfigurationException("unmarshall url failed!", ex));
        }
        return sgt;
    }

    public ScrapeGroupsType unMarshall(String path) {
        log.debug("unmarshall path -- " + path);
        ScrapeGroupsType sgt = null;
        File file = new File(path);
        sgt = unMarshall(file);
        return sgt;
    }
}
